package com.chenglu.android;

import androidx.fragment.app.Fragment;


import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.chenglu.android.bean.TabEvent;
import com.chenglu.android.cart.CartFragment;
import com.chenglu.android.classify.ClassifyTreeFragment;
import com.chenglu.android.common.base.BaseMvpActivity;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.SPUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.common.widget.NotScrollViewPager;
import com.chenglu.android.index.IndexFragment;
import com.chenglu.android.index.selectcity.citypicker.model.LocateState;
import com.chenglu.android.index.selectcity.citypicker.utils.StringUtils;
import com.chenglu.android.message.MessageFragment;
import com.chenglu.android.mine.me.MineFragment;
import com.chenglu.android.redpacket.RedPacketFragement;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import me.jessyan.autosize.internal.CustomAdapt;

/**
 * @author: lyj
 * @Created: by Copyright 2023/3/7 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：底部框架
 */

public class MainActivity extends BaseMvpActivity  {


    NotScrollViewPager mainViewPager;
    CommonTabLayout mainTablayout;
    // 第一次按下返回键的事件
    private long firstPressedTime;
    private String mTitles[] = new String[]{"首页", "消息","红包","购物车", "我的"};

    private int tabPosition = 0;
    //自定义的Icon
    private int[] mTabIcon = new int[]{
            R.drawable.tab_icon_home_default,
            R.drawable.tab_icon_message_default,
            R.drawable.tab_icon_red_packet_default,
            R.drawable.tab_icon_shopping_cart_default,
            R.drawable.tab_icon_mine_default
    };
    //自定义选择器 tab_bar_fudou_h
    private int[] mTabIconSelected = new int[]{
            R.drawable.tab_icon_home_preesed,
            R.drawable.tab_icon_message_preesed,
            R.drawable.tab_icon_red_packet_default,
            R.drawable.tab_icon_shopping_cart_preesed,
            R.drawable.tab_icon_mine_preesed
    };

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    ComnonPagerAdapter mComnonPagerAdapter;

    @Override
    protected int getLayoutId() {

        return R.layout.activity_main;
    }



//    @Override
//    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//        initView();
//    }

    @Override
    protected void initView() {
        mainViewPager = findViewById(R.id.main_view_pager);
        mainTablayout = findViewById(R.id.main_tablayout);

        QbSdk.initX5Environment(mContext, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                // 内核初始化完成，可能为系统内核，也可能为系统内核
            }

            /**
             * 预初始化结束
             * 由于X5内核体积较大，需要依赖网络动态下发，所以当内核不存在的时候，默认会回调false，此时将会使用系统内核代替
             * @param isX5 是否使用X5内核
             */
            @Override
            public void onViewInitFinished(boolean isX5) {

            }
        });

        //初始化操作
        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);

        //状态类颜色
        setImmersionBarColor(R.color.white,true);

        initFragment();



    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initFragment() {
        List<Fragment> list = new ArrayList<>();
        list.add(IndexFragment.newInstance());
        list.add(MessageFragment.newInstance());
        list.add(RedPacketFragement.newInstance());
        list.add(CartFragment.newInstance(false));
        list.add(MineFragment.newInstance());
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mTabIconSelected[i], mTabIcon[i]));
        }
        mainTablayout.setTabData(mTabEntities);
        mComnonPagerAdapter = new ComnonPagerAdapter(getSupportFragmentManager(), list);
        mainViewPager.setAdapter(mComnonPagerAdapter);
        mainViewPager.setOffscreenPageLimit(list.size());
        mainTablayout.setCurrentTab(0);
//        mainTablayout.setCurrentTab(0);
        mainViewPager.setCurrentItem(0, false);
        mainTablayout.setOnTabSelectListener(new OnTabSelectListener() {

            @Override
            public void onTabSelect(int position) {
                LogUtils.e("mainTablayout- position:" + position);

                switch (position) {
                    case 0:
//                        ImmersionBar.with(MainActivity.this).
//                                keyboardEnable(true)
//                                .navigationBarColor(R.color.white)
//                                .statusBarDarkFont(false)
//                                .init();

                        tabPosition= position;
                        mainViewPager.setCurrentItem(position, false);
                        mainTablayout.setCurrentTab(position);
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        tabPosition= position;
                        mainViewPager.setCurrentItem(position, false);
                        mainTablayout.setCurrentTab(position);
                        break;
//                    case 3:
//                        tabPosition= position;
//                        mainViewPager.setCurrentItem(position, false);
//                        mainTablayout.setCurrentTab(position);
//                        break;
//                    case 1:
//                        isLogin= SPUtils.getBoolean("ISLOGIN",false);
//                        if (isLogin) {
//                            tabPosition=position;
//                            mainViewPager.setCurrentItem(position, false);
//                            mainTablayout.setCurrentTab(position);
//                        } else {
//                            mainViewPager.setCurrentItem(tabPosition, false);
//                            mainTablayout.setCurrentTab(tabPosition);
////                            startActivity(new Intent(MainActivity.this, LoginActivity.class)
////                                    .putExtra(LoginActivity.EXTRA_TABPOSITION, position));
//                        }
//                        break;
//                    case 2:
//                        isLogin= SPUtils.getBoolean("ISLOGIN",false);
//                        if (isLogin) {
//                            tabPosition=position;
//                            mainViewPager.setCurrentItem(position, false);
//                            mainTablayout.setCurrentTab(position);
//                        } else {
//                            mainViewPager.setCurrentItem(tabPosition, false);
//                            mainTablayout.setCurrentTab(tabPosition);
//
////                            startActivity(new Intent(MainActivity.this, LoginActivity.class)
////                                    .putExtra(LoginActivity.EXTRA_TABPOSITION, position));
//
//                        }
//                        break;
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }




    private Callback mCallback;

    public interface Callback{
        void setLocationTitle();
    }

    //一个给接口变量赋值的方法
    public void setmCallback(Callback callback){
        mCallback=callback;
    }

    @Override
    protected boolean registerEventBus() {
        return true;
    }



    /**
     * 子类接受事件 重写该方法
     *
     * @param event 事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(TabEvent event) {
        LogUtils.d("ggggggggg");
        mainTablayout.setCurrentTab(event.getTab());
        mainViewPager.setCurrentItem(event.getTab(), false);
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - firstPressedTime < 2000) {
            super.onBackPressed();

        } else {
            ToastUtils.show("再按一次退出");
            firstPressedTime = System.currentTimeMillis();
        }
    }



    //    头条适配方案的实现方法
//    @Override
//    public boolean isBaseOnWidth() {
//        return false;
//    }
//
//    @Override
//    public float getSizeInDp() {
//        return 812;
//    }



}